package com.cy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class ConsumerApp {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApp.class,args);
    }
    @Value("${server.port}")
    private String server;
//    构建远程过程调用对象，基于对象访问远程服务
    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

    @RestController
    public class ConsumerController{
        @Autowired
        private RestTemplate restTemplate;
// 负载均衡客户端对象：
// 基于此对象可以从注册中心获取服务实例
        @Autowired
        private LoadBalancerClient loadBalancerClient;

        @GetMapping("/consumer/doRestEcho1")
        public String doRestEcho01(){
            //访问localhost:8081/provider/echo/{msg}
            String url = "http://localhost:8081/provider/echo/"+server;
            return restTemplate.getForObject(url,String.class);
        }
    }
}
